//JavaScript Framework 2.0 Code
try {
    Type.registerNamespace('com.yonyou.ncmm_pac_pda.PurchaseQueryController');
    com.yonyou.ncmm_pac_pda.PurchaseQueryController = function() {
        com.yonyou.ncmm_pac_pda.PurchaseQueryController.initializeBase(this);
        this.initialize();
    }
    function com$yonyou$ncmm_pac_pda$PurchaseQueryController$initialize() {
        //you can programing by $ctx API
        //get the context data through $ctx.get()
        //set the context data through $ctx.push(json)
        //set the field of the context through $ctx.put(fieldName, fieldValue)
        //get the parameter of the context through $ctx.param(parameterName)
        //Demo Code:
        //    var str = $ctx.getString();      //获取当前Context对应的字符串
        //    alert($ctx.getString())          //alert当前Context对应的字符串
        //    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
        //    json["x"] = "a";        //为当前json增加字段
        //    json["y"] = [];           //为当前json增加数组
        //    $ctx.push(json);            //设置context，并自动调用数据绑定
        //
        //    put方法需手动调用databind()
        //    var x = $ctx.get("x");    //获取x字段值
        //    $ctx.put("x", "b");     //设置x字段值
        //    $ctx.put("x", "b");     //设置x字段值
        //    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
        //    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
        //    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
        //    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组

        //your initialize code below...

    }

    function com$yonyou$ncmm_pac_pda$PurchaseQueryController$evaljs(js) {
        eval(js)
    }

    function com$yonyou$ncmm_pac_pda$PurchaseQueryController$button0_onclick(sender, args) {
      $view.close();
    }

    function showMenuList(sender, args) {
        NCMMPUINUtil.showMenuLists("imagebutton1");
    }

    function queryNCPuInData(sender, args) {
        $ctx.dataCollect();
        var pk_org = NCMMPUINUtil.getDefaultStockPK();
        if (pk_org == null || pk_org == "") {
            $toast("请先在业务设置中设置默认库存组织");
            return false;
        }
        if (isDateNotSameNull()) {
            $toast("起始和结束日期需要同时输入");
            return;
        }
        var condition = {}; 
        pk_org = "='" + pk_org + "'";      
        condition["pk_org"]=pk_org; 
        //物料
        if ($ctx.getString("pk_srcmaterial") != null && $ctx.getString("pk_srcmaterial") != "") {
            var value = getWhereValue("pk_srcmaterial", "in");
            condition = $.extend({}, condition, {
                "pk_srcmaterial" : value
            });
        }
        //单据号
        if ($ctx.getString("vbillcode") != null && $ctx.getString("vbillcode") != "") {
             //处理反斜杠
            var value =  NCMMPubUtil.htmlspecialchars2UI($ctx.getString("vbillcode"));
            //处理单引号
            value = NCMMPUINUtil.htmlsqmchars2UI(value);
            value=" = '" +  value+ "'";
            condition = $.extend({}, condition, {
                "vbillcode" : value
            });
        }
        //采购部门
        if ($ctx.getString("pk_dept") != null && $ctx.getString("pk_dept") != "") {
            var value = getWhereValue("pk_dept", "in");
            condition = $.extend({}, condition, {
                "pk_dept" : value
            });
        }
        //供应商
        if ($ctx.getString("pk_supplier") != null && $ctx.getString("pk_supplier") != "") {
            var value = getWhereValue("pk_supplier", "in");
            condition = $.extend({}, condition, {
                "pk_supplier" : value
            });
        }
        //供应商批次号
        if ($ctx.getString("vvendbatchcode") != null && $ctx.getString("vvendbatchcode") != "") {
            //var value = getWhereValue("", "like");
             //处理反斜杠
            var value =  NCMMPubUtil.htmlspecialchars2UI($ctx.getString("vvendbatchcode"));
            //处理单引号
            value = NCMMPUINUtil.htmlsqmchars2UI(value);
            value=" = '" +  value+ "'";
            condition = $.extend({}, condition, {
                "vvendbatchcode" : value
            });
        }
        //批次号
        if ($ctx.getString("batchcode") != null && $ctx.getString("batchcode") != "") {
            //处理反斜杠
            var value =  NCMMPubUtil.htmlspecialchars2UI($ctx.getString("batchcode"));
            //处理单引号
            value = NCMMPUINUtil.htmlsqmchars2UI(value);
            value=" = '" +  value+ "'";
            condition = $.extend({}, condition, {
                "batchcode" : value
            });
            
        }
        //单据日期
        if ($ctx.getString("dbillbegindate") != null && $ctx.getString("dbillbegindate") != "") {
            var value = getWhereDateValue("dbilldate", "dbillbegindate", "dbillendate");
            condition = $.extend({}, condition, {
                "dbilldate" : value
            });
        }
        //计划到货日期
        if ($ctx.getString("dplanreceivedateb") != null && $ctx.getString("dplanreceivedateb") != "") {
            var value = getWhereDateValue("dplanreceivedate", "dplanreceivedateb", "dplanreceivedatee");
            condition = $.extend({}, condition, {
                "dplanreceivedate" : value
            });
        }
        //$alert($jsonToString(condition));
        $view.closeWithCallBack({
            "qcondition": condition
        });
        
    }

    function getWhereValue(bindf, calcode) {
        var tmp;
        if (calcode == "in") {
            tmp = " in ('" + $jsonToString($ctx.getString(bindf)) + "')";
        } else if (calcode == "like") {
            tmp = " like '%" + $jsonToString($ctx.getString(bindf)) + "%'";
        }
        return tmp;
    }

    function getWhereDateValue(bindf, bgdate, enddate) {
        var begindate = $jsonToString($ctx.getString(bgdate));
        var enddate = $jsonToString($ctx.getString(enddate));
        var tmp = "between '" + begindate + " 00:00:00'" + " and '" + enddate + " 23:59:59'";
        return tmp;
    }

    function isDateNotSameNull() {
        return false;
    }

    function showMaterialRef(sender, args) {
        if (!isRefEmpty("PDAMATERIAL")) {          
            var refinfo = {
                reftitle : "物料",
                reftablename : "app_bd_material"
            };
            $view.open({
                viewid : "com.yonyou.ncmm_pac_pda.RefPage",
                isKeep : "true",
                data : refinfo,
                callback : function() {
                    var refdata = $param.getJSONObject("data");
                    $ctx.put("materialname", refdata.name);
                    $ctx.put("pk_srcmaterial", refdata.pk_material)
                    $ctx.dataBind();
                }
            });
        }

    }

    function showDeptRef(sender, args) {
        if (!isRefEmpty("PDADEPT")) {
            // 打开部门参照页面
            var refinfo = {
                reftitle : "部门",
                reftablename : "app_org_dept"
            };
            $view.open({
                viewid : "com.yonyou.ncmm_pac_pda.RefPage",
                isKeep : "true",
                data : refinfo,
                callback : function() {
                    var refdata = $param.getJSONObject("data");
                    $ctx.put("deptname", refdata.name);
                    $ctx.put("pk_dept", refdata.pk_dept);
                    $ctx.dataBind();
                }
            });
        }
    }

    //判断参照表中是否有数据
    function isRefEmpty(reftype) {
        var condition = {
            "ncbillcode" : reftype
        };
        var rtn;
        NCMMPubUtil.queryVOByConditions(AppmappingheadVO, condition, function(data) {
            var jsondata = {
                bdlistdata : eval(data)
            };          
            var reftitle = (jsondata.bdlistdata)[0]["appbillname"];
            if ((jsondata.bdlistdata)[0]["downloadnum"] == "" || (jsondata.bdlistdata)[0]["downloadnum"] == "0") {
                $alert("【" + reftitle + "】没有可参照的数据");
                rtn= true;                             
            }
            else{
                 rtn= false;
             }
        });
       return rtn;
    }

    function showSupplierRef(sender, args) {
        if (!isRefEmpty("PDASUPPLIER")) {          
            // 打开供应商参照页面
            var refinfo = {
                reftitle : "供应商",
                reftablename : "app_supplier"
            };
            $view.open({
                viewid : "com.yonyou.ncmm_pac_pda.RefPage",
                isKeep : "true",
                data : refinfo,
                callback : function() {
                    var refdata = $param.getJSONObject("data");
                    $ctx.put("suppliername", refdata.name);
                    $ctx.put("pk_supplier", refdata.pk_supplier)
                    $ctx.dataBind();
                }
            });
        }
    }

    function showBatchcodeRef(sender, args) {
        // 打开批次号参照页面
        var refinfo = {
            reftitle : "批次号",
            reftablename : "app_scm_batchcode"
        };

        $view.open({
            viewid : "com.yonyou.ncmm_pac_pda.RefPage",
            isKeep : "true",
            data : refinfo,
            callback : function() {
                var refdata = $param.getJSONObject("data");
                $ctx.put("batchcode", refdata.vbatchcode);
                $ctx.put("pk_batchcode", refdata.pk_batchcode)
                $alert("pk_batchcode：" + $jsonToString($ctx.getString("pk_batchcode")));
                $ctx.dataBind();
            }
        });

    }

    function clearctx(sender, args) {
        //物料
        $ctx.put("materialname", "");
        $id("textbox0").set("value", "");

        //单据号
        $id("textbox1").set("value", "");
        $ctx.put("vbillcode", "");
        //采购部门
        $ctx.put("deptname", "");
        $id("textbox2").set("value", "");

        //供应商

        $id("textbox3").set("value", "");
        $ctx.put("suppliername", "");
        //供应商批次号
        $id("textbox6").set("value", "");
        $ctx.put("bc_vvendbatchcode", "");
        //批次号
        $id("textbox4").set("value", "");
        $ctx.put("batchcode", "");
        //单据日期
        $ctx.put("dbillbegindate", "");
        $id("dateinput0").set("value", "");
        $ctx.put("dbillendate", "");
        $id("dateinput1").set("value", "");
        //计划到货日期
        $ctx.put("dplanreceivedateb", "");
        $id("dateinput2").set("value", "");
        $ctx.put("dplanreceivedatee", "");
        $id("dateinput3").set("value", "");

        $ctx.dataBind();

    }

    function initQueryPage(sender, args) {
        //单据日期
        $ctx.put("dbillbegindate", NCMMDateUtil.getDay());
        $ctx.put("dbillendate", NCMMDateUtil.getDay());
        $ctx.dataBind();

    }


    com.yonyou.ncmm_pac_pda.PurchaseQueryController.prototype = {

        button0_onclick : com$yonyou$ncmm_pac_pda$PurchaseQueryController$button0_onclick,
        initialize : com$yonyou$ncmm_pac_pda$PurchaseQueryController$initialize,
        evaljs : com$yonyou$ncmm_pac_pda$PurchaseQueryController$evaljs
    };
    com.yonyou.ncmm_pac_pda.PurchaseQueryController.registerClass('com.yonyou.ncmm_pac_pda.PurchaseQueryController', UMP.UI.Mvc.Controller);
} catch(e) {
    $e(e);
}